package ylsb.controller;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ylsb.model.YltpXiangmu;
import ylsb.service.YltpXiangmuService;
import ylsb.support.JsonResult;
import ylsb.support.PageInfo;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("xiangmu")
public class YltpXiangmuController {

    @Resource
    YltpXiangmuService ddService;

    public Logger log = LoggerFactory.getLogger(YltpXiangmuController.class);

    @RequestMapping("findAllBypage")
    public JsonResult<?> findVaildByAdminId(@RequestParam("xiangmuname") String ppXiangmuName,
                                            @RequestParam("pageindex") int ppPageIndex,
                                            @RequestParam("pagesize") int ppPageSize){
        try {
            if(!ppXiangmuName.equals("")) {
                ppXiangmuName = "%" + ppXiangmuName + "%";
            }
            List<Map<String, Object>> mmList=ddService.findAllBypage(ppXiangmuName,ppPageIndex,ppPageSize);
            Integer mmCount = ddService.getCount(ppXiangmuName);
            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);
            Map<String, Object> mmMap = new HashMap<String, Object>();
            mmMap.put("XiangMuList", mmList);
            mmMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmMap);

        }catch (Exception e) {
            e.printStackTrace();
            log.error("xiangmu/findAllBypage:error",e);
            return JsonResult.getErrorResult("xiangmu/findAllBypage:error " + e.getMessage());
        }
    }

    @RequestMapping("add")
    public JsonResult<?> add(@RequestParam("xiangmuname") String ppxiangmuname,
                             @RequestParam("xiangmuleixing") String ppxiangmuleixing,
                             @RequestParam("meetingtime") String ppmeetingtime,
                             @RequestParam("tongguoshu") int pptongguoshu,
                             @RequestParam("zhuangjiashu") int ppzhuangjiashu){
        try {

            SimpleDateFormat simdate=new SimpleDateFormat("yyyy-MM-dd");

            Date mmmeetingtime=simdate.parse(ppmeetingtime);

            YltpXiangmu mmYltpXiangmu=new YltpXiangmu();
            mmYltpXiangmu.setXiangmuid(UUID.randomUUID().toString());
            mmYltpXiangmu.setXiangmuname(ppxiangmuname);
            mmYltpXiangmu.setXiangmuleixing(ppxiangmuleixing);
            mmYltpXiangmu.setZhuangjiashu(ppzhuangjiashu);
            mmYltpXiangmu.setTongguoshu(pptongguoshu);
            mmYltpXiangmu.setMeetingtime(mmmeetingtime);
            mmYltpXiangmu.setStatus(100);
            mmYltpXiangmu.setCreatetime(new Date());
            mmYltpXiangmu.setIsactived(0);
            return ddService.add(mmYltpXiangmu)==true? JsonResult.getSuccessResult("新增项目成功")
                    : JsonResult.getErrorResult("新增项目失败");
        }catch (Exception e) {
            e.printStackTrace();
            log.error("xiangmu/add:error",e);
            return JsonResult.getErrorResult("xiangmu/add:error " + e.getMessage());
        }

    }

    @RequestMapping("find_one")
    public JsonResult<?> find_one(@RequestParam("xianmguid") String ppxianmguId) {
        try {
            return JsonResult.getSuccessResult(ddService.findOne(ppxianmguId));

        }catch (Exception e) {
            e.printStackTrace();
            log.error("xiangmu/find_one:error",e);
            return JsonResult.getErrorResult("xiangmu/find_one:error " + e.getMessage());
        }
    }

    @RequestMapping("getIsactivedxiangmu")
    public JsonResult<?> getIsactivedxiangmu(){
        try {
            return JsonResult.getSuccessResult(ddService.getIsactivedxiangmu());
        }catch (Exception e) {
            e.printStackTrace();
            log.error("xiangmu/getIsactivedxiangmu:error",e);
            return JsonResult.getErrorResult("xiangmu/getIsactivedxiangmu:error " + e.getMessage());
        }
    }


    @RequestMapping("modify")
    public JsonResult<?> modify(@RequestParam("xianmguid") String ppxianmguId,
                                @RequestParam("xiangmuname") String ppxiangmuname,
                                @RequestParam("xiangmuleixing") String ppxiangmuleixing,
                                @RequestParam("meetingtime") String ppmeetingtime,
                                @RequestParam("tongguoshu") int pptongguoshu,
                                @RequestParam("zhuangjiashu") int ppzhuangjiashu){
        try {
            SimpleDateFormat simdate=new SimpleDateFormat("yyyy-MM-dd");

            Date mmmeetingtime=simdate.parse(ppmeetingtime);
            YltpXiangmu mmYltpXiangmu=new YltpXiangmu();
            mmYltpXiangmu.setXiangmuid(ppxianmguId);
            mmYltpXiangmu.setXiangmuname(ppxiangmuname);
            mmYltpXiangmu.setXiangmuleixing(ppxiangmuleixing);
            mmYltpXiangmu.setZhuangjiashu(ppzhuangjiashu);
            mmYltpXiangmu.setTongguoshu(pptongguoshu);
            mmYltpXiangmu.setMeetingtime(mmmeetingtime);
            return ddService.modify(mmYltpXiangmu)==true? JsonResult.getSuccessResult("修改项目成功")
                    : JsonResult.getErrorResult("修改项目失败");

        }catch (Exception e) {
            e.printStackTrace();
            log.error("xiangmu/modify:error",e);
            return JsonResult.getErrorResult("xiangmu/modify:error " + e.getMessage());
        }
    }

    @RequestMapping("delete")
    public JsonResult<?> delete(@RequestParam("xianmguid") String ppxianmguId){
        try {
            YltpXiangmu mmYltpXiangmu=new YltpXiangmu();
            mmYltpXiangmu.setXiangmuid(ppxianmguId);
            mmYltpXiangmu.setStatus(200);
            return ddService.modify(mmYltpXiangmu)==true? JsonResult.getSuccessResult("删除项目成功")
                    : JsonResult.getErrorResult("删除项目失败");

        }catch (Exception e) {
            e.printStackTrace();
            log.error("xiangmu/delete:error",e);
            return JsonResult.getErrorResult("xiangmu/delete:error " + e.getMessage());
        }
    }

    @RequestMapping("modifyisactived")
    public JsonResult<?> modifyisactive(@RequestParam("xiangmuid") String ppxiangmuId){
        try {

            return ddService.modifyisactived(ppxiangmuId) == true ? JsonResult.getSuccessResult("修改成功")
                    : JsonResult.getErrorResult("修改失败");

        } catch (Exception e) {
            e.printStackTrace();
            log.error("xiangmu/modifyisactived:error",e);
            return JsonResult.getErrorResult("xiangmu/modifyisactived:error " + e.getMessage());
        }
    }


}
